package com.gowants.pems.service;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import org.springframework.web.multipart.MultipartFile;

import com.gowants.pems.mapper.SysLogMapper;
import com.gowants.pems.mapper.SysUserMapper;
import com.gowants.pems.model.SysNewLog;
import com.gowants.pems.model.SysUser;
import com.gowants.pems.model.SysLog;
import com.gowants.pems.util.DateUtil;
import com.gowants.pems.util.FileTypeJudge;
import com.gowants.pems.util.UploadUtil;
import com.gowants.pems.mapper.SysNewLogMapper;
import com.gowants.pems.service.ParamsService;

@Service(value = "sysUserService")
public class SysUserService extends NewBaseService<SysUser> {
	@Autowired
	private SysUserMapper sysUserMapper;
	@Autowired
	private SysNewLogMapper SysNewLogMapper;
	@Autowired
	private SysLogMapper sysLogMapper;
	@Autowired
	private ParamsService paramsService;
	@Override
	public int save(SysUser record) {
		// TODO Auto-generated method stub
		return sysUserMapper.save(record);
	}

	@Override
	public int delete(String primaryKey) {
		// TODO Auto-generated method stub
		return sysUserMapper.delete(primaryKey);
	}

	@Override
	public int update(SysUser record) {
		// TODO Auto-generated method stub
		return sysUserMapper.update(record);
	}

	@Override
	public SysUser selectOne(String primaryKey) {
		// TODO Auto-generated method stub
		return sysUserMapper.selectOne(primaryKey);
	}

	@Override
	public List<SysUser> select(SysUser record) {
		// TODO Auto-generated method stub
		return sysUserMapper.select(record);
	}

	@Override
	public int getCount(SysUser record) {
		// TODO Auto-generated method stub
		return 0;
	}
	public int selectRepetition(SysUser record) {
		// TODO Auto-generated method stub
		return sysUserMapper.selectRepetition(record);
	}
	public String  saveCheck(SysUser record) {
		// TODO Auto-generated method stub
		String code="";
		if(StringUtils.isBlank(record.getUserName()))
		{
			code="TSMS_0000207";
			return code;
		}
		if(StringUtils.isBlank(record.getName()))
		{
			code="TSMS_0000206";
			return code;
		}
		if(StringUtils.isBlank(record.getPwd()))
		{
			code="TSMS_0000208";
			return code;
		}
		//验证姓名
		if(!isChinese(record.getName())) {
			code="TSMS_0000424";
			return code;
		}
		if(record.getName().length()>6) {
			code="TSMS_0000425";
			return code;
		}
		if(!isPhone(record.getPhone()))
		{
			code="TSMS_0000212";
			return code;
		}
		int count0= selectRepetition(record);
		if(count0>0)
		{
			code = "TSMS_0000076";
			return code;
		}
		
		return code;
	}
	public static boolean isPhone(String phone) {
		String regex = "^((13[0-9])|(14[5,6,7,9])|(15([0-3]|[5-9]))|(16[5,6])|(17[0,1,3,5,6,7,8])|(18[0-9])|(19[8|9]))\\d{8}$";
		if (phone.length() != 11) {

			return false;
		} else {
			Pattern p = Pattern.compile(regex);
			Matcher m = p.matcher(phone);
			boolean isMatch = m.matches();
			if (!isMatch) {

			}
			return isMatch;
		}
	}
	/**
	 * 判断该字符串是否为中文
	 * @param string
	 * @return
	 */
	public static boolean isChinese(String string){
	    int n = 0;
	    for(int i = 0; i < string.length(); i++) {
	        n = (int)string.charAt(i);
	        if(!(19968 <= n && n <40869)) {
	            return false;
	        }
	    }
	    return true;
	}
	/**
	 * <上传文件校验大小及格式>
	 *
	 * @param
	 * @return -1 大小不符合规定 0 上传类型不符合规定 1 校验通过
	 * @remake create by GJR 2019/7/1 11:31
	 */
	public int uploadCheck(MultipartFile myFile, String fileType, String fileSize) {
		if (!uploadFileSizeCheck(myFile, fileSize)) {
			return -1;
		}

		if (!uploadFileTypeCheck(myFile, fileType)) {
			return 0;
		}

		return 1;
	}

	/**
	 * <上传文件校验>
	 *
	 * @param
	 * @return
	 * @remake
	 */
	public boolean uploadFileSizeCheck(MultipartFile myFile, String fileSize) {
		// 文件大小不符合规定
		String size = paramsService.getParamValueByNumber(fileSize);

		// 未定义视为没限制
		if (null == size || "".equals(size)) {
			return true;
		}

		// 字节单位比较，数据库配置单位是MB
		if (myFile.getSize() > Long.valueOf(size) * 1024 * 1024) {
			return false;
		}

		return true;
	}

	/**
	 * <文件格式校验>
	 *
	 * @param
	 * @return
	 * @remake
	 */
	public boolean uploadFileTypeCheck(MultipartFile myFile, String fileType) {

		// 图片格式不符合规定
		String fileName = UploadUtil.getFileName(myFile);

		// 格式正则配置
		String type = paramsService.getParamValueByNumber(fileType);

		// 未定义视为没限制
		if (null == type || "".equals(type)) {
			return true;
		}

		if (!fileName.matches(type)) {
			return false;
		}
		return true;

	}

	public String downloadPath(String pathConfig) {

		String path = paramsService.getParamValueByNumber(pathConfig);
		return path;
	}

	/**
	 * <文件上传>
	 *
	 * @param myFile 文件 pathConfig 文件保存根路径 isRelpaceFileName 是否更换文件名 isCreateDir
	 *               是否创建新文件夹 isRelpaceFileName true: 文件名_时间戳 isCreateDir true:
	 *               按当前时间创建文件夹
	 * @return
	 * @remake
	 */
	public String upload(MultipartFile myFile, String pathConfig, String relativePath, String mappingPath,
			boolean isRelpaceFileName, boolean isCreateDir) {
		String result = "";
		String fileName = UploadUtil.getFileName(myFile);
		try {
			// 设置文件全限定名
			String path = paramsService.getParamValueByNumber(pathConfig);
			// 是否更换文件名
			if (isRelpaceFileName) {
				// fileName.
				// String[] fNames = fileName.split("\\.");
				SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");// 设置日期格式
				Random rand = new Random();
				int x = rand.nextInt(999);
				String fNames = fileName.substring(fileName.lastIndexOf(".") + 1);// 后缀
				fileName = df.format(new Date()) + x + "." + fNames;

			}

			// 是否创建文件夹
			if (isCreateDir) {
				String dir = DateUtil.getDays();
				fileName = dir + "/" + fileName;
			}
			// 拼接保存文件的全限定名
			String filePath = "";

			if (StringUtils.isNotBlank(relativePath)) {
				filePath += ("/" + relativePath);
			}
			filePath += ("/" + fileName);

			UploadUtil.saveFileFromInputStream(myFile.getInputStream(), path + filePath);
			// result = filePath.replace(path, "");
			result = filePath;
		} catch (Exception e) {
			e.printStackTrace();
		}

		return result;
	}
	public String uploadNew(MultipartFile myFile, String pathConfig, String relativePath, String mappingPath,
			boolean isRelpaceFileName, boolean isCreateDir) {
		String result = "";
		try {
			String fileName = UploadUtil.getFileName(myFile);
			// 设置文件全限定名
			String path = paramsService.getParamValueByNumber(pathConfig);
			// 是否更换文件名
			if (isRelpaceFileName) {
				// fileName.
				// String[] fNames = fileName.split("\\.");
				SimpleDateFormat df = new SimpleDateFormat("YYYYMMddHHmmss");// 设置日期格式
				Random rand = new Random();
				int x = rand.nextInt(999);
				String fNames = fileName.substring(fileName.lastIndexOf(".") + 1);// 后缀
				fileName = df.format(new Date()) + x + "." + fNames;

			}

			// 是否创建文件夹
			if (isCreateDir) {
				String dir = DateUtil.getDays();
				fileName = dir + "/" + fileName;
			}
			// 拼接保存文件的全限定名
			String filePath = "";

			if (StringUtils.isNotBlank(relativePath)) {
				filePath += ("/" + relativePath);
			}
			filePath += ("/" + fileName);
			FileTypeJudge.resize1(myFile.getInputStream(), path + filePath);
			UploadUtil.saveFileFromInputStream(myFile.getInputStream(), path + filePath);
			result = filePath;

		} catch (Exception e) {
			return "xxxxxxxxxx";

		}

		return result;

	}
	public int saveLog(SysNewLog record) {

		return SysNewLogMapper.save(record);
	}

	public int saveSysLog(SysLog record) {

		return sysLogMapper.save(record);
	}
	
	public List<SysUser> login(SysUser record) {
		// return ScmsUserMapper.selectUserName(userName);
		return sysUserMapper.login(record);
	}
	public SysUser selectUserName(String userName) {
		// return ScmsUserMapper.selectUserName(userName);
		return sysUserMapper.selectUserName(userName);
	}
}
